#pragma once

#include "KnowledgeBaseInterpreterInterface.h"
#include "KnowledgeBaseMemory.h"

class KnowledgeBaseInterpreter;
typedef Factory < KnowledgeBaseInterpreterInterface >::CastedPtr < KnowledgeBaseInterpreter > KnowledgeBaseInterpreterPtr;

class KnowledgeBaseInterpreter
: public KnowledgeBaseInterpreterInterface
{
	friend class Factory < KnowledgeBaseInterpreterInterface >;
public:
	static KnowledgeBaseInterpreterInterfacePtr Spawn ( KnowledgeBaseMemoryInterfacePtr memory );
	static void CheckInnocence ();
private:
	KnowledgeBaseInterpreter ();
	virtual ~KnowledgeBaseInterpreter ();
	virtual void Interpret ( std::string command );
	void ReadNextToken ();
	void ReadNewTypeToken ();
	void ReadTextToken ();
	void SkipSpaces ();
	void AddNewTypeElement ();
private:
	enum TokenType
	{
		TOKEN_EMPTY,
		TOKEN_NEW_TYPE,
		TOKEN_TEXT
	};
	
	KnowledgeBaseMemoryInterfacePtr memory;
	KnowledgeBaseElementInterfacePtr nullElement;
	KnowledgeBaseElementInterfacePtr typeRootElement;
	KnowledgeBaseElementInterfacePtr valueRootElement;
	std::string command;
	std::string token;
	TokenType tokenType;
	std::vector < KnowledgeBaseElementInterfacePtr > appendedTypeElements;
	static Factory < KnowledgeBaseInterpreterInterface > factory;
};
